﻿using UnityEngine;
using System.Collections;

public class TrapMovement : MonoBehaviour 
{
	public Transform platform;
	public Transform startTransform;
	public Transform endTransform;

	public float platformSpeed;

	private Vector3 direction;
	private Transform destination;

	void Start()
	{
		SetDestination (startTransform);
	}

	void FixedUpdate()
	{
		platform.rigidbody.MovePosition(platform.position + direction * platformSpeed * Time.fixedDeltaTime); // move in this direction

		// if the distance between the platform current location and its destination < its speed, then
		if(Vector3.Distance (platform.position, destination.position) < platformSpeed * Time.fixedDeltaTime)
		{
			if (destination == startTransform) //SetDestination(destination == startTransform ? endTransform : startTransform);
			{
				SetDestination(endTransform);
			}
			else
			{
				SetDestination(startTransform);
			}
		}
	}
	
	void OnDrawGizmos()
	{
		Gizmos.color = Color.green; // make the wireframe of the startTransform green
		Gizmos.DrawWireCube (startTransform.position, platform.localScale); // draw the wireframe

		Gizmos.color = Color.red; // make the wireframe of the endTransform red
		Gizmos.DrawWireCube (endTransform.position, platform.localScale); // draw the wireframe
	}

	void SetDestination(Transform dest)
	{
		destination = dest;
		direction = (destination.position - platform.position).normalized; // which direction to go
	}
}